using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using System.Collections;

namespace FBPChaos
{
	public class TerrainGrid
	{
		
		List<Monster> monsters;
        List<TerrainFeature> features;
		Wizard[] wizards;

               
        const int width = 15;
        const int height = 12;
        
        public TerrainGrid(List<Monster> listOfMonsters, Wizard[] listOfWizards, List<TerrainFeature> ListOfFeatures)
        {
            monsters = listOfMonsters;
            wizards = listOfWizards;
            features = ListOfFeatures;
        }
        
        public int Height
        {
            get { return height; }
        }

        public int Width
        {
            get { return width; }
        }

        /// <summary>
        /// Returns the top monster in the stack (may be dead)
        /// Returns null if cell is empty
        /// </summary>
        /// <param name="point">Cell being queried</param>
        /// <returns>Top monster in stack</returns>
        public Monster getMonster(Point point)
        {
            int highestZIndex = -1;
            int arrayIndex = -1;
            
            for (int counter = 0; counter < monsters.Count; counter++)
            {
                if (monsters[counter].Position == point)
                {
                    if (monsters[counter].ZIndex > highestZIndex)
                    {
                        highestZIndex = monsters[counter].ZIndex;
                        arrayIndex = counter;
                    }
                }
            }
            if (arrayIndex != -1)
                return monsters[arrayIndex];
            else
                return null;
        }


        /// <summary>
        /// Checks to see if a cell is occupied based on x and y coordinates
        /// </summary>
        /// <param name="x">X Coordinate</param>
        /// <param name="y">Y Coordinate</param>
        /// <returns>Returns true if an item is contained within the cell, otherwise false</returns>
        public bool checkOccupied(Point point)
		{
            foreach (Monster monster in monsters)
            {
                if (monster.Position == point)
                    return true;
            }
            foreach (Wizard wizard in wizards)
            {
                if (wizard.Position == point)
                    return true;
            }
            return false;
		}

        /// <summary>
        /// 
        /// </summary>
        /// <returns>-1 if unoccupied, wizard number if occupied</returns>
        public int checkOwner(Point point)
		{
			throw new NotImplementedException();
		}

        public Wizard getWizard(Point point)
        {
            for (int counter = 0; counter < wizards.Length; counter++)
            {
                if (wizards[counter].Position == point)
                    return wizards[counter];
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
		public EntityType checkType(Point point)
		{
            //Last in z-index will always be a wizard
            //Then monsters
            //Then objects (e.g. blobs which may be on monsters or walls which may be on dead creatures

            if (getMonster(point) != null)
                return EntityType.Monster;
            else if (getWizard(point) != null)
                return EntityType.Wizard;
            else
                return EntityType.None;
		}
	}
}
